package ui;


import java.awt.Color;

import java.awt.Cursor;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import dao.StudentDao;
import dao.StudentDaoJDBCImpl;
import dao.TeacherDao;
import dao.TeacherDaoJDBCImpl;







public class LoginFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    private JButton loginButton;
    private JButton returnButton;
    private JLabel nameLabel;
    private JLabel pwdLabel;
    private JLabel lblNewLabel;
    private JPanel panel;
    private JTextField numberField;
    private JPasswordField passwordField;
    
    @SuppressWarnings("unused")
	private String identity;
    //public static void main(String[] args) {
    	/*EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    // 启动登陆界面
                    CatLogin frame = new CatLogin();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });*/
    	 /*try {
             // 启动登陆界面
             //Login frame = new Login();
             //frame.setVisible(true);
    		 List<Student> stulist = new ArrayList<>();
    	     //Student student = new Student("马艺芳","男","1999-01-31");
    	     //student.setMath(80.0);
    	     //student.setJava(70.0);
    	     //student.setSport(90.0);
    	     StudentDao stu = new StudentDaoJDBCImpl();
    	     //stu.searchByname("my");
    	     int id = 2017211098;
    	     //student.setStudentID(id);
    	     //stu.searchByid(id);
    	     //stu.diplayAllStudent();
    	     //stu.deleteByid(id);
    	     //stulist = stu.getAllStudents();
    	     //for(int i = 0;i<stulist.size();i++) {
    	    	 //System.out.println(stulist.get(i).toString());
    	     //}
    	    
         } catch (Exception e) {
             e.printStackTrace();
         }

    }*/
    
    
	 public LoginFrame(String identity) {
	        
		 	this.identity = identity;
	    	if(identity.equals("Teacher"))
	    		setTitle("教师登录");
	    	else setTitle("学生登录");
	        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        setBounds(700, 300, 500, 400);
	        //setSize(500, 400);

	        panel = new JPanel() {
	            private static final long serialVersionUID = 1L;
	            @Override//铺设背景图片
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                g.drawImage(new ImageIcon(
	                        "images/图片4.png").getImage(), 0,
	                        0, getWidth(), getHeight(), null);
	            }
	        };
	        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
	        setContentPane(panel);
	        panel.setLayout(null);
	        
	        //用户名输入框
	        numberField = new JTextField();
	        numberField.setFont(new Font( "Serif", 1, 25));//设置字体
	        //textField.setBounds(197, 376, 190, 50);
	        numberField.setBounds(170, 130, 150, 40);
	        numberField.setOpaque(false);//此方法是设置控件是否透明的。true表示不透明，false表示透明。文本区域设为透明
	        panel.add(numberField);
	        numberField.setColumns(10);
	        
	        //密码输入框
	        passwordField = new JPasswordField();
	        passwordField.setFont(new Font( "Serif", 1, 25));//设置字体
	        passwordField.setEchoChar('*');//设置显示字符
	        passwordField.setOpaque(false);//文本区域设为透明
	        passwordField.setBounds(170, 190, 150, 40);
	        panel.add(passwordField);
	        
	        //登录按钮
	        loginButton = new JButton("登录");
	        loginButton.setIcon(new ImageIcon("images\\图片2.png"));//设置图标背景
	        loginButton.setBounds(110, 260, 100, 50);
	        loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置鼠标在该区域显示状态变化
	        getRootPane().setDefaultButton(loginButton);//设置回车响应
	        panel.add(loginButton);
	        
	        //返回按钮
	        returnButton = new JButton("返回");
	        returnButton.setIcon(new ImageIcon("images\\图片3.png"));//设置图标背景
	        returnButton.setBounds(290, 260, 100, 50);
	        returnButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置鼠标在该区域显示状态变化
	        panel.add(returnButton);
	        
	        if(identity.equals("Teacher"))
	        	nameLabel = new JLabel("用户名"); 
	        else nameLabel = new JLabel("学号");
	        nameLabel.setFont(new Font( "Serif", 1, 20));
	        nameLabel.setBounds(105, 130, 150, 40); 
	    	panel.add(nameLabel);  
	    	
	    	pwdLabel = new JLabel("密码"); 
	    	pwdLabel.setFont(new Font( "Serif", 1, 20));
	    	pwdLabel.setBounds(115, 190, 150, 40); 
	    	panel.add(pwdLabel);  
	        
	    	// 提示信息
	        lblNewLabel = new JLabel();
	        lblNewLabel.setBounds(170, 230, 160, 30);
	        lblNewLabel.setFont(new Font("Dialog", 1,  15));
	        lblNewLabel.setForeground(Color.red);
	        panel.add(lblNewLabel);
	        
	        	
	        	   // 监听登陆按钮
	 	       loginButton.addActionListener(new ActionListener() {
	 	            public void actionPerformed(ActionEvent e) {
	 	            	
	 	            String name = numberField.getText();//获取用户输入的用户名    
	 	            String pwd = new String(passwordField.getPassword());//获取用户输入的密码
	 	            
	 	            
	 	            try {
	 	                //调用用户登录方法
	 	            	if(identity.equals("Teacher")) {
	 	            		
	 	            		TeacherDao td = new TeacherDaoJDBCImpl();
	 	            		if(td.isLogin(identity,name,pwd)==true) {
		 	                	JOptionPane.showMessageDialog(null, "登录成功！");	
		 	                	TeacherFrame teacherframe = new TeacherFrame(name);
		 	                	teacherframe.setVisible(true);
		 	                	setVisible(false);// 隐藏掉登陆界面
		 	                }
		 	                else {
		 	                	//给出用户提示信息，并清空信息栏
		                         lblNewLabel.setText("您输入的密码有误！");
		                         numberField.setText("");
		                         passwordField.setText("");
		                         numberField.requestFocus();
		 	                }
 	                		
 	                	}
	 	            	else if(identity.equals("Student")) {
	 	            		int id = Integer.parseInt(name);
	 	            		StudentDao ud = new StudentDaoJDBCImpl();
	 	            		
	 	            		if(ud.isLogin(identity,id,pwd)==true) {
		 	                	JOptionPane.showMessageDialog(null, "登录成功！");	
		 	                	StudentFrame studentframe = new StudentFrame(id);
		 	                	studentframe.setVisible(true);
		 	                	setVisible(false);// 隐藏掉登陆界面
		 	                }
		 	                else {
		 	                	//给出用户提示信息，并清空信息栏
		                         lblNewLabel.setText("您输入的密码有误！");
		                         numberField.setText("");
		                         passwordField.setText("");
		                         numberField.requestFocus();
		 	                }
 	                		
 	                	}
	 	                
	 	                
	 	            } catch (Exception e1) {
	 	                
	 	                e1.printStackTrace();
	 	            }
	 	                
	 	                
	 	            }
	 	        });
	        
	 	      returnButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					
					Userschoice frame = new Userschoice();
					frame.setVisible(true);
					dispose();
					
					
				}
	 	    	  
	 	      });
	  

	       
	 }
}
